package variabilitysmelldetection;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;

import variabilitysmelldetection.measurement.models.ComponentItem;
import variabilitysmelldetection.measurement.models.Item;
import variabilitysmelldetection.measurement.models.MethodItem;
import variabilitysmelldetection.measurement.models.Project;

public class WriteCSV {
	public static final String OUTPUT_FOLDER_NAME       = "vsd-output";
	public static final String CLASSES_FILE_NAME        = File.separator+"classes.csv";
	public static final String METHODS_FILE_NAME        = File.separator+"methods.csv";
	public static final String PROJECT_FILE_NAME        = File.separator+"project.csv";
	public static final String GODMETHODS_FILE_NAME     = File.separator+"godmethods.csv";
	public static final String GODCLASSES_FILE_NAME     = File.separator+"godclasses.csv";
	public static final String SHOTGUNSURGERY_FILE_NAME = File.separator+"shotgun-surgery.csv";
	
	public static boolean writeMethods(Project project){
		boolean ok = false;
		try{
			File output = getOutputFolder(project);
			List<ComponentItem> classes = project.getClassItems();
	
			BufferedWriter bw = new BufferedWriter(new FileWriter(output.getAbsolutePath()+METHODS_FILE_NAME));
			boolean header = false;
			for (ComponentItem c : classes) {
				for (Item m : c.getMethods()) {
					if (!header) {
						bw.write(m.getFieldsAndMeasuresNames());
						bw.newLine();
						header = true;
					}
					bw.write(m.toCSV());
					bw.newLine();
				}
			}
			bw.close();
			ok = true;
		}catch (Exception e){
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean writeClasses(Project project){
		boolean ok = false;
		try{
			List<ComponentItem> classes = project.getClassItems();
			if (!classes.isEmpty()) {
				File output = getOutputFolder(project);
	
				BufferedWriter bw = new BufferedWriter(new FileWriter(output.getAbsolutePath()+CLASSES_FILE_NAME));
				bw.write(classes.get(0).getFieldsAndMeasuresNames());
				bw.newLine();
				for (ComponentItem c : classes) {				
					bw.write(c.toCSV());
					bw.newLine();
				}
				bw.close();
				ok = true;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean writeProject(Project project) {
		boolean ok = false;
		try{
			File output = getOutputFolder(project);

			BufferedWriter bw = new BufferedWriter(new FileWriter(output.getAbsolutePath()+PROJECT_FILE_NAME));
			bw.write(project.getFieldsAndMeasuresNames());
			bw.newLine();
			bw.write(project.toCSV());
			bw.close();
			ok = true;

		}catch (Exception e){
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean writeGodMethods(Project project, List<MethodItem> godMethods) {
		boolean ok = false;
		try{
			File output = getOutputFolder(project);

			BufferedWriter bw = new BufferedWriter(new FileWriter(output.getAbsolutePath()+GODMETHODS_FILE_NAME));
			boolean header = false;
			for (MethodItem m : godMethods) {
				if (!header) {
					bw.write(m.getFieldsAndMeasuresNames());
					bw.newLine();
					header = true;
				}
				bw.write(m.toCSV());
				bw.newLine();
			}
			bw.close();
			ok = true;
		}catch (Exception e){
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean writeGodClasses(Project project, List<ComponentItem> godClasses) {
		boolean ok = false;
		try{
			File output = getOutputFolder(project);

			BufferedWriter bw = new BufferedWriter(new FileWriter(output.getAbsolutePath()+GODCLASSES_FILE_NAME));
			boolean header = false;
			for (ComponentItem m : godClasses) {
				if (!header) {
					bw.write(m.getFieldsAndMeasuresNames());
					bw.newLine();
					header = true;
				}
				bw.write(m.toCSV());
				bw.newLine();
			}
			bw.close();
			ok = true;
		}catch (Exception e){
			e.printStackTrace();
		}
		
		return ok;
	}
	
	public static boolean writeShotgunSurgery(Project project, List<ComponentItem> godClasses) {
		boolean ok = false;
		try{
			File output = getOutputFolder(project);

			BufferedWriter bw = new BufferedWriter(new FileWriter(output.getAbsolutePath()+SHOTGUNSURGERY_FILE_NAME));
			boolean header = false;
			for (ComponentItem m : godClasses) {
				if (!header) {
					bw.write(m.getFieldsAndMeasuresNames());
					bw.newLine();
					header = true;
				}
				bw.write(m.toCSV());
				bw.newLine();
			}
			bw.close();
			ok = true;
		}catch (Exception e){
			e.printStackTrace();
		}
		
		return ok;
	}
	
	private static File getOutputFolder(final Project project){
		File output = new File(project.getProjectPath()+File.separator+OUTPUT_FOLDER_NAME);
		if (!output.exists() || !output.isDirectory()) {
			output.mkdirs();
		}
		return output;
	}
}
